﻿Public Class ArticleDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Count_user As Integer = validateUser()
        Ratingcount()
        If Count_user = 1 Then
            rbl_rating.Visible = False
            btn_submit_rating.Visible = False
            yourrating()
        Else
            rbl_rating.Visible = True
            btn_submit_rating.Visible = True
        End If

    End Sub

    Protected Sub btn_Addcomment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Addcomment.Click
        Dim A_ID As Integer = Request.QueryString("ID")
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim sql As String = " INSERT INTO Comments (Article_ID, UserID, Comment_Date, Comment) VALUES (@v4, @v1, @v2, @v3)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v4", A_ID)
        cmd.Parameters.AddWithValue("@v1", Uid)
        cmd.Parameters.AddWithValue("@v2", Today.Date())
        cmd.Parameters.AddWithValue("@v3", txt_comment.Text)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("ArticleDetails.aspx?ID=" & A_ID)
    End Sub
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function

    Protected Sub btn_submit_rating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit_rating.Click
        Dim A_ID As Integer = Request.QueryString("ID")
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim sql As String = " INSERT INTO ArticleRating (AR_ArticleID, AR_rating, AR_UserID) VALUES (@v1, @v2, @v3)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", A_ID)
        cmd.Parameters.AddWithValue("@v2", rbl_rating.SelectedValue)
        cmd.Parameters.AddWithValue("@v3", Uid)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("ArticleDetails.aspx?ID=" & A_ID)
    End Sub
    Protected Sub Ratingcount()
        Dim ArticleID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) AS CountR, Avg(AR_Rating) AS ARating from ArticleRating Where AR_ArticleID = " & ArticleID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            lbl_rating_Users.Text = CStr(Dreader("CountR"))
            lbl_Avg_Rating.Text = CStr(Dreader("ARating"))
        Catch
        End Try
    End Sub
    Protected Sub yourrating()
        Dim ArticleID As Integer = CInt(Request.QueryString("ID"))
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select AR_Rating from ArticleRating Where AR_ArticleID = " & ArticleID & " And AR_UserID = " & Uid & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        lbl_r_by_u.Text = "Your Rating: " + CStr(Dreader("AR_Rating")) + "<br /> Thank you"

    End Sub
    Protected Function validateUser() As Integer
        Dim ArticleID As Integer = CInt(Request.QueryString("ID"))
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) AS Count_User from ArticleRating Where AR_ArticleID = " & ArticleID & " AND AR_UserID = " & Uid & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        Dim C_Count As Integer = Dreader("Count_User")
        Return C_Count
    End Function
End Class